<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>绑定/解绑</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<jsp:include page="${pageContext.request.contextPath }/resource_path.jsp" flush="true"/>
	<style type="text/css">
		#bind{
			width: 300px;
			height: 150px;
			background-color: green;
			margin-bottom: 20px;
			text-align: center;
		}
		#unbind{
			width: 300px;
			height: 150px;
			background-color: red;
			text-align: center;
			color:#FFF;
			font-size: 50px;
			line-height: 150px;
			vertical-align: center;
		}
		#ob{
			width: 300px;
			height:320px;
			margin: 200px auto;
			text-align: center;
			color:#FFF;
			font-size: 50px;
			line-height: 150px;
			vertical-align: center;
		}
	</style>
</head>
<body class="layui-anim layui-anim-up">
	<div id="formDiv" hidden>
		<div class="box">
			<form class="layui-form layui-card-body" id="addform">
				<div class="layui-form-item">
					<div class="layui-block">
						<label class="layui-form-label">托盘条码</label>
						<div class="layui-input-inline" style="width: 150px">
							<input class="layui-input" id="tray_code" name="tray_code" type="text" />
						</div>
					</div>
				</div>
				
				<div class="layui-form-item">
					<label class="layui-form-label">物料产品码</label>
					<div class="layui-input-inline" style="width: 150px">
						<input class="layui-input" id="materiel_num" name="materiel_num" type="text" />
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">包装类型</label>
					<div class="layui-input-inline" style="width: 150px">
						<select style="width: 150px" class="" name="packType" id="packType" lay-filter="packType">
							<option value="">--请选择包装类型--</option>
							<option value="0">大箱</option>
							<option value="1">小箱</option>
						</select>
					</div>
					<label class="layui-form-label">规格/型号</label>
					<div class="layui-input-inline" style="width: 150px">
						<select style="width: 150px" class="" name="sizeAndPre" id="sizeAndPre" lay-filter="sizeAndPre">
							<option value="">--请选择规格/型号--</option>
						</select>
					</div>
					<label class="layui-form-label">批次</label>
					<div class="layui-input-inline" style="width: 150px">
						<select style="width: 150px" class="" name="mBatch" id="mBatch" lay-filter="mBatch">
							<option value="">--请选择批次--</option>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-block">
						<label class="layui-form-label">物料条码</label>
						<div class="layui-input-inline" style="width: 690px">
							<textarea class="layui-textarea" id="materiel_code" name="materiel_code"></textarea>
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn  layui-btn-blue" lay-submit lay-filter="addform" style="margin-left:130px" id="subform">绑定</button>
						<button class="layui-btn layui-btn-primary" style="margin-left:200px">取消</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div id="formDiv2" hidden>
		<div class="box">
			<form class="layui-form layui-card-body" id="addform2">
				<div class="layui-form-item">
					<div class="layui-block">
						<label class="layui-form-label">托盘条码</label>
						<div class="layui-input-inline" style="width: 150px">
							<input class="layui-input" id="tray_code2" name="tray_code" type="text" />
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-block">
						<label class="layui-form-label">物料条码</label>
						<div class="layui-input-inline" style="width: 690px">
							<textarea class="layui-textarea" id="materiel_code2" name="materiel_code"></textarea>
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn  layui-btn-blue" lay-submit lay-filter="addform2" style="margin-left:130px" id="subform2">解绑</button>
						<button class="layui-btn layui-btn-primary" style="margin-left:200px">取消</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div class="x-nav">
		<span class="layui-breadcrumb">
  			<a href="">首页</a>
			<a>
				<cite>绑定/解绑</cite>
			</a>
		</span>
    </div>
	<div style="width:100%;height:100%">
		<div id="ob">
			<div id="bind">
				<span>绑定</span>
			</div>
			<div id="unbind">
				<span>解绑</span>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		//日期格式转换
		function date2String(timestamp){
			var d = new Date(timestamp);
			var date = (d.getFullYear()) + "-" + 
		       (d.getMonth() + 1<10?"0"+(d.getMonth() + 1):d.getMonth() + 1) + "-" +
		       (d.getDate()<10?"0"+d.getDate():d.getDate());
		       return date;
		}
		layui.use(['table','layer','upload','form','laydate'], function(){
			var table = layui.table;
			var layer = layui.layer;
			var form = layui.form;
			var laydate = layui.laydate;
			
			$("#bind").click(function(){
				layer.open({
	                type: 1 				//Page层类型
	                ,area: ['900px', ''] 	//宽  高
	                ,title: '绑定'
	                ,shade: 0.6 			//遮罩透明度
	                ,maxmin: true 			//允许全屏最小化
	                ,anim: 1 				//0-6的动画形式，-1不开启
	                ,content:$('#formDiv')
	                ,success:function(){
	                	document.getElementById("addform").reset();
	                	form.render();
	                }
    			});
			})
			$("#unbind").click(function(){
				layer.open({
	                type: 1 				//Page层类型
	                ,area: ['900px', ''] 	//宽  高
	                ,title: '解绑'
	                ,shade: 0.6 			//遮罩透明度
	                ,maxmin: true 			//允许全屏最小化
	                ,anim: 1 				//0-6的动画形式，-1不开启
	                ,content:$('#formDiv2')
	                ,success:function(){
	                	document.getElementById("addform2").reset();
	                	form.render();
	                }
    			});
			})
			//物料产品码改变事件
			$("#materiel_num").change(function(){
				var materiel_num = $("#materiel_num").val();
				materiel_num = "'"+materiel_num+"'";
				var empty = "''";
				var strWhere = '';
				strWhere = '(m.materiel_num = '+materiel_num+' or m.brevity_num = '+materiel_num+') and '
				//通过产品码查批次
				$.ajax({
					type:'post'
					,url:'${pageContext.request.contextPath }/inventoryManagement/selectInventoryInfoByCode.do'
					,data:{'strWhere':strWhere}
					,dataType:'json'
					,success:function(res){
						$("#mBatch").find("option").not(":first").remove();
						for(var i = 0;i < res.length;i++){
							if(i-1 == -1){
								$("#mBatch").append('<option value="'+res[i].mBatch+'">'+res[i].mBatch+'</option>');
							}else if(res[i].mBatch != res[i-1].mBatch){
								$("#mBatch").append('<option value="'+res[i].mBatch+'">'+res[i].mBatch+'</option>');
							}
						}
						form.render();
					}
				})
				//通过产品码查规格型号
				$.ajax({
					type:'post'
					,url:'${pageContext.request.contextPath }/inventoryManagement/selectMaterielSizeByCode.do'
					,data:{'strWhere':strWhere}
					,dataType:'json'
					,success:function(res){
						$("#sizeAndPre").find("option").not(":first").remove();
						for(var i = 0;i < res.length;i++){
							$("#sizeAndPre").append('<option value="'+res[i].materiel_size+'/'+res[i].materiel_properties+'">'+res[i].materiel_size+'/'+res[i].materiel_properties+'</option>');
						}
						form.render();
					}
				})
			})
			//绑定托盘物料
	        form.on('submit(addform)', function (data) {
	        	$("#subform").attr("disabled",true);
	        	$.ajax({
	        		url : '${pageContext.request.contextPath }/bindAndUnbind/bindingMaterielAndTray.do',
	       			data : $('#addform').serialize(),
	       			type : "post",
	       			//dataType : "json",
	    			}).done(
	    				function(res) {
	    		  			if(res>0){
	    		  				toastr.success('绑定成功！');
	    	                    setTimeout(function(){
	    	                        location.reload();
	    	                    },2000);
	    		  			}else if(res == -2){
	    		  				toastr.error('绑定失败，请绑定同种物料');
	    		  				$("#subform").attr("disabled",false);
	    		  			}else{
	    		  				toastr.error('绑定失败！');
	    		  				$("#subform").attr("disabled",false);
	    		  			}
	    				}
	    			)
	    			return false;
	        });
			//解绑托盘物料
	        form.on('submit(addform2)', function (data) {
	        	$("#subform2").attr("disabled",true);
	        	$.ajax({
	        		url : '${pageContext.request.contextPath }/bindAndUnbind/unbindMaterielAndTray.do',
	       			data : $('#addform2').serialize(),
	       			type : "post",
	       			//dataType : "json",
	    			}).done(
	    				function(res) {
	    		  			if(res>0){
	    		  				toastr.success('解绑成功！');
	    	                    setTimeout(function(){
	    	                        location.reload();
	    	                    },2000);
	    		  			}else{
	    		  				toastr.error('解绑失败！');
	    		  				$("#subform2").attr("disabled",false);
	    		  			}
	    				}
	    			)
	    			return false;
	        });
		});
		toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
	</script>
</body>

</html>